var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        goodsAttributeDialogVisible: false,
        stepPriceDialogVisible: false,
        title: null,
        goodsCraftPrice: {},
        goodsCraftAttributeData: [],// 基础商品属性table
        goodsCraftData: [],// 基础商品工艺
        goodsCraftDataRow: null, // 当前行工艺
        goodsTemplateData: [], //基础商品模板
        goodsTemplateDataRow: null, //当前商品模板行
        goodsCraftOptionData: [],// 基础商品工艺选项table
        stepPriceData: [],// 阶梯价格table
        stepPriceRow: null, //当前阶梯价所在option 行
    },

    mounted: function () {
        this.initData();
    },

    updated: function () {
        //将已经存在的赋值选中
        for (var i = 0; i < vm.goodsCraftOptionData.length; i++) {
            var temp = vm.goodsCraftOptionData[i];
            if (temp.craftPriceId) {
                this.$refs.goodsCraftOptionTable.toggleRowSelection(temp, true);
            }
        }
    },

    methods: {
        /*页面初始化数据*/
        initData: function () {
            // 请求已经存在的 商品工艺价格
            $.get(baseURL + "goodsCraftPrice/goodscraftprice/getGoodsTemplateCraftPriceList", function (r) {
                vm.goodsCraftAttributeData = r.goodsCraftPriceData.goodsCraftList;

                //重新赋值stepPrice
                if (r.goodsCraftPriceData.goodsCraftPriceList) {
                    for (var i = 0; i < r.goodsCraftPriceData.goodsCraftPriceList.length; i++) {
                        var temp = r.goodsCraftPriceData.goodsCraftPriceList[i];
                        if (temp.stepPrice) {
                            temp.stepPrice = JSON.parse(temp.stepPrice);
                        }
                    }
                }
                vm.goodsTemplateData = r.goodsCraftPriceData.goodsTemplateList;
                vm.goodsCraftOptionData = r.goodsCraftPriceData.goodsCraftPriceList;

            });
        },

        // 添加商品工艺
        addGoodsCraft: function () {
            // 查询商品工艺
            $.get(baseURL + "goodsCraftPrice/goodscraftprice/getGoodsCraftList", function (r) {
                vm.goodsCraftData = r.goodsCraftList;
                vm.goodsAttributeDialogVisible = true;
            });

        },

        // 添加当前行到左侧 基础商品属性表
        addToLeftTable: function (index) {
            var temp = vm.goodsCraftData[index];
            var selected = vm.goodsCraftAttributeData.find(function (option) {
                return option.attributeId === temp.attributeId;
            })
            if (selected) {
                this.$message.error('该工艺已经添加 !');
                return;
            }
            this.$refs.goodsCraftTable.toggleRowSelection(temp, true);
            vm.goodsCraftAttributeData.push(temp);

        },

        // 添加选中的到当前行
        addSelectedToLeftTable: function () {
            if (this.$refs.goodsCraftTable.selection.length == 0) {
                this.$message.error('至少选中一个工艺 !');
                return;
            }

            for (var index in this.$refs.goodsCraftTable.selection) {
                var temp = this.$refs.goodsCraftTable.selection[index];
                var selected = vm.goodsCraftAttributeData.find(function (option) {
                    return option.attributeId === temp.attributeId;
                })
                if (!selected) {
                    vm.goodsCraftAttributeData.push(temp);
                }
            }

            vm.goodsAttributeDialogVisible = false;
        },

        // 工艺当前行 点击事件
        goodsCraftAttributeClicked: function (row, event, column) {
            var craftName = row.attributeName;
            var self = this;
            // 查询商品模板
            $.get(baseURL + "goodsCraftPrice/goodscraftprice/getGoodsTemplateList?attributeId=" + row.attributeId, function (r) {
                // 获取到该工艺的所有模板列表
                if (r.code == 0){
                    if (r.goodsTemplateList.length > 0){
                        vm.goodsTemplateData = r.goodsTemplateList;
                        // 赋值当前工艺行
                        vm.goodsCraftDataRow = row;
                    }else {
                        self.$message({type: 'info', message: craftName +" 暂无模板!"});
                    }
                }else {
                    alert(r.msg);
                }

            });

        },

        // 模板当前行点击事件
        goodsTemplateClicked: function (row, event, column) {
            if (vm.goodsCraftDataRow == null) {
                this.$message({type: 'info', message: '请先点击工艺行'});
                return;
            }
            // 解析商品模板 返回选项
            var data = {'attributeId': vm.goodsCraftDataRow.attributeId, 'goodsTemplate': row};
            $.ajax({
                type: "POST",
                url: baseURL + "goodsCraftPrice/goodscraftprice/getGoodsTemplateOptionList",
                contentType: "application/json",
                data: JSON.stringify(data),
                success: function (r) {
                    if (r.code == 0) {
                        vm.goodsCraftOptionData = r.goodsCraftOptionPriceList;
                        //重新赋值stepPrice
                        if (r.goodsCraftOptionPriceList) {
                            for (var i = 0; i < r.goodsCraftOptionPriceList.length; i++) {
                                var temp = r.goodsCraftOptionPriceList[i];
                                if (temp.stepPrice) {
                                    temp.stepPrice = JSON.parse(temp.stepPrice);
                                }
                            }
                        }
                        vm.goodsTemplateDataRow = row;
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },

        /*设置阶梯价格*/
        openStepPriceDialog: function (row) {
            //先初始化数据
            if (row.stepPrice &&  row.stepPrice.length > 0) {
                vm.stepPriceData = row.stepPrice;

            } else {
                vm.stepPriceData = [];
                var temp = {"beginNum": null, "endNum": null, "singlePrice": null, "remark": null};
                // 加入一条空白数据
                vm.stepPriceData.push(temp);
            }

            // 保留当前option 所在行
            vm.stepPriceRow = row;
            vm.stepPriceDialogVisible = true;
        },

        /*保存阶梯价*/
        saveStepPrice: function () {
            // 数据校验
            for(var i=0; i<vm.stepPriceData.length; i++){
                if(!vm.stepPriceData[i].beginNum  && !vm.stepPriceData[i].endNum  &&
                    !vm.stepPriceData[i].singlePrice ){
                    //去除空元素
                    vm.stepPriceData.splice(i,1);
                }
            }

            var row = vm.stepPriceRow;

            row.stepPrice = vm.stepPriceData;

            // 清空临时数据
            vm.stepPriceRow = null;
            vm.stepPriceData = [];
            vm.stepPriceDialogVisible = false;
        },

        /*添加一行阶梯价*/
        addOneStepPrice: function () {
            var temp = {"beginNum": null, "endNum": null, "singlePrice": null, "remark": null};
            vm.stepPriceData.push(temp);
        },

        /*删除一行数据*/
        deleteOneStepPrice: function (index, rows) {
            if (rows.length > 1) {
                rows.splice(index, 1);
            }
        },

        /*一键设置价格为第一行价格*/
        oneKeySetPrice: function () {
            this.$confirm('确定要把所有选项价格一键设置为第一行价格吗', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 取出第一行数据
                for (var i = 0; i < vm.goodsCraftOptionData.length; i++) {
                    // 从第二行开始 赋值

                    var temp = vm.goodsCraftOptionData[i];
                    // 已经有价格的跳过去
                    if (!temp.craftPriceId){
                        temp.unitPrice = vm.goodsCraftOptionData[0].unitPrice;
                        temp.minUnitPrice = vm.goodsCraftOptionData[0].minUnitPrice;
                        temp.minBeginPrice = vm.goodsCraftOptionData[0].minBeginPrice;
                        temp.floatCoefficient = vm.goodsCraftOptionData[0].floatCoefficient;
                        temp.stepPrice = vm.goodsCraftOptionData[0].stepPrice;
                        temp.fixedPrice = vm.goodsCraftOptionData[0].fixedPrice;
                        this.$refs.goodsCraftOptionTable.toggleRowSelection(temp, true);
                    }else {
                        this.$refs.goodsCraftOptionTable.toggleRowSelection(temp, false);
                    }

                }


            }).catch(() => {
                this.$message({type: 'info', message: '已取消一键设价'});
            })

        },

        //选中选项
        goodsCraftOptionSelected: function (selection, row) {
            var selected = selection.length && selection.indexOf(row) !== -1;
            if (selected) {
                // 如果是选中 则把 几个为null的价格设置为 0
                row.unitPrice = row.unitPrice == null ? 0 : row.unitPrice;
                row.minUnitPrice = row.minUnitPrice == null ? 0 : row.minUnitPrice;
                row.minBeginPrice = row.minBeginPrice == null ? 0 : row.minBeginPrice;
                row.fixedPrice = row.fixedPrice == null ? 0 : row.fixedPrice;
                row.floatCoefficient = row.floatCoefficient == null ? 1 : row.floatCoefficient;
                // 如果同时为0 给出提示
                if (row.unitPrice == 0 && row.minUnitPrice == 0 &&
                    row.minBeginPrice == 0 && row.fixedPrice == 0 &&
                    row.floatCoefficient == 1){
                    this.$message({type: 'info', message: '所勾选价格全部为0 请注意确认 !'});
                }
            }

        },

        //全选
        goodsCraftOptionSelectedAll: function (selection) {
            if (selection.length >0){//全选中
                for (var i=0; i< selection.length;i++){
                    var row = selection[i];
                    // 循环将null转为0
                    row.unitPrice = row.unitPrice == null ? 0 : row.unitPrice;
                    row.minUnitPrice = row.minUnitPrice == null ? 0 : row.minUnitPrice;
                    row.minBeginPrice = row.minBeginPrice == null ? 0 : row.minBeginPrice;
                    row.fixedPrice = row.fixedPrice == null ? 0 : row.fixedPrice;
                    row.floatCoefficient = row.floatCoefficient == null ? 0 : row.floatCoefficient;
                    // 如果同时为0 给出提示
                    if (row.unitPrice == 0 && row.minUnitPrice == 0 &&
                        row.minBeginPrice == 0 && row.fixedPrice == 0 &&
                        row.floatCoefficient == 0){
                        this.$message({type: 'info', message: row.optionName +"-"+row.influenceName +"所在行价格都为0 请注意查看!"});
                    }
                }

            }

        },


        /*保存基础商品工艺价格*/
        saveGoodsCraftPrice: function () {
            var postData = [];
            // 先拷贝一个数据 避免页面数据错误
            var newSelectionObject = jQuery.extend(true, [], this.$refs.goodsCraftOptionTable.selection);
            for (var index  in newSelectionObject) {
                var currentSelection = newSelectionObject[index];
                if (currentSelection.stepPrice) {
                    currentSelection.stepPrice = JSON.stringify(currentSelection.stepPrice);
                }
                // 放入当前模板id
                currentSelection.templateId = vm.goodsTemplateDataRow.templateId;
                postData.push(currentSelection);
            }

            if (postData.length == 0) {
                this.$message.error('至少选中一个工艺选项 !');
                return;
            }
            if (postData.length > 30) {
                this.$message.error('一次最多添加30个工艺选项价格 !');
                return;
            }
            var self = this;

            $.ajax({
                type: "POST",
                url: baseURL + "goodsCraftPrice/goodscraftprice/save",
                contentType: "application/json",
                data: JSON.stringify({"craftPriceList": postData}),
                success: function (r) {
                    if (r.code === 0) {
                        self.$message({message: '保存成功 !', type: 'success'});
                    } else {
                        alert(r.msg);
                    }
                }
            });


        },

        /*根据工艺删除价格*/
        deleteCraftAttribute: function (index, rows) {

            var self = this;
            var row = rows[index];
            // 判断后台是否 已经存在价格
            if (row.craftPriceId) {
                //存在id 则请求后台删除 此处id 仅代表存在价格 并不是该工艺的价格id
                this.$confirm('确定要删除' + row.attributeName + "价格吗?", '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.ajax({
                        type: "get",
                        url: baseURL + "goodsCraftPrice/goodscraftprice/deleteByAttributeId?attributeId=" + row.attributeId,
                        success: function (r) {
                            if (r.code == 0) {
                                self.$message({message: '删除成功 !', type: 'success'});
                                self.initData();
                                /*rows.splice(index, 1);
                                vm.goodsCraftOptionData = [];*/
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }).catch(() => {
                    this.$message({type: 'info', message: '已取消删除'});
                })
            } else {
                // 仅仅删除页面数据即可
                rows.splice(index, 1);
                vm.goodsCraftOptionData = [];
            }

        },

        /*根据选中工艺价格删除*/
        deleteCraftPrice: function () {
            // 取出所有选中的工艺价格
            var selected = this.$refs.goodsCraftOptionTable.selection;
            if (selected.length == 0) {
                this.$message.error('至少选中一个工艺价格 !');
                return;
            }
            var postIds = [];
            var selectedCraftPriceIds = [];
            for (var index in selected) { //循环选中项
                if (selected[index].craftPriceId) {
                    //后台存在真是数据
                    postIds.push(selected[index].craftPriceId);
                    selectedCraftPriceIds.push(selected[index]);
                } else {
                    vm.goodsCraftOptionData.splice($.inArray(selected[index], vm.goodsCraftOptionData), 1)
                    //仅前台页面数据 直接删除即可
                }
            }

            var self = this;
            if (postIds.length > 0) {
                // 后台ajax 请求删除
                this.$confirm('确定要删除已配价选项吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.ajax({
                        type: "POST",
                        url: baseURL + "goodsCraftPrice/goodscraftprice/deleteByIds",
                        contentType: "application/json",
                        data: JSON.stringify(postIds),
                        success: function (r) {
                            if (r.code == 0) {
                                self.$message({message: '删除成功 !', type: 'success'});
                                self.initData();
                                // 此处循环删除页面
                                /*  for(var selectedCraftPriceId in selectedCraftPriceIds){
                                      vm.goodsCraftOptionData.splice($.inArray(selectedCraftPriceId, vm.goodsCraftOptionData), 1)
                                  }*/
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }).catch(() => {
                    this.$message({type: 'info', message: '已取消删除'});
                })
            }

        },


    }
});